<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: ActiveRecord::Schema</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">ActiveRecord::Schema</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activerecord/lib/active_record/schema_rb.html">
                vendor/rails/activerecord/lib/active_record/schema.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="Migration.html">
                Migration
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Allows programmers to programmatically <a
href="Schema.html#M001375">define</a> a schema in a portable DSL. This
means you can <a href="Schema.html#M001375">define</a> tables, indexes,
etc. without using SQL directly, so your applications can more easily
support multiple databases.
</p>
<p>
Usage:
</p>
<pre>
  ActiveRecord::Schema.define do
    create_table :authors do |t|
      t.string :name, :null =&gt; false
    end

    add_index :authors, :name, :unique

    create_table :posts do |t|
      t.integer :author_id, :null =&gt; false
      t.string :subject
      t.text :body
      t.boolean :private, :default =&gt; false
    end

    add_index :posts, :author_id
  end
</pre>
<p>
<a href="Schema.html">ActiveRecord::Schema</a> is only supported by
database adapters that also support migrations, the two features being very
similar.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001375">define</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M001375" class="method-detail">
        <a name="M001375"></a>

        <div class="method-heading">
          <a href="#M001375" class="method-signature">
          <span class="method-name">define</span><span class="method-args">(info={}, &amp;block)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Eval the given block. All methods available to the current connection
adapter are available within the block, so you can easily use the database
definition DSL to build up your schema (create_table, add_index, etc.).
</p>
<p>
The <tt>info</tt> hash is optional, and if given is used to <a
href="Schema.html#M001375">define</a> metadata about the current schema
(like the schema&#8216;s version):
</p>
<pre>
  ActiveRecord::Schema.define(:version =&gt; 15) do
    ...
  end
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001375-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001375-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/schema.rb, line 42</span>
42:     <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">define</span>(<span class="ruby-identifier">info</span>={}, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
43:       <span class="ruby-identifier">instance_eval</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
44: 
45:       <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">info</span>.<span class="ruby-identifier">empty?</span>
46:         <span class="ruby-identifier">initialize_schema_information</span>
47:         <span class="ruby-identifier">cols</span> = <span class="ruby-identifier">columns</span>(<span class="ruby-value str">'schema_info'</span>)
48: 
49:         <span class="ruby-identifier">info</span> = <span class="ruby-identifier">info</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
50:           <span class="ruby-identifier">v</span> = <span class="ruby-constant">Base</span>.<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">quote</span>(<span class="ruby-identifier">v</span>, <span class="ruby-identifier">cols</span>.<span class="ruby-identifier">detect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">c</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">k</span>.<span class="ruby-identifier">to_s</span> })
51:           <span class="ruby-node">&quot;#{k} = #{v}&quot;</span>
52:         <span class="ruby-keyword kw">end</span>
53: 
54:         <span class="ruby-constant">Base</span>.<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">update</span> <span class="ruby-node">&quot;UPDATE #{Migrator.schema_info_table_name} SET #{info.join(&quot;, &quot;)}&quot;</span>
55:       <span class="ruby-keyword kw">end</span>
56:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>